package dearchenpush.good.com.qianfanlive.modules.live.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import dearchenpush.good.com.qianfanlive.R;
import dearchenpush.good.com.qianfanlive.modules.live.adapter.LiveInAdapter;
import dearchenpush.good.com.qianfanlive.modules.live.bean.LiveInBean;
import dearchenpush.good.com.qianfanlive.ui.LiveActivity;
import dearchenpush.good.com.qianfanlive.url.UrlConfigs;
import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class LiveInFragment extends Fragment implements View.OnClickListener{
    private String path;
    private RecyclerView recyclerView;
    private StaggeredGridLayoutManager layoutManager;
    private LiveInAdapter adapter;
    private List<LiveInBean.MessageBean.AnchorsBean> data = new ArrayList<>();
    private int page=0;


    private int index;



    public LiveInFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_live_in, container, false);
        Bundle bundle = getArguments();
        String url = bundle.getString("url");
        index = bundle.getInt("page");


        //  path = String.format(url, 0, 0);
        path = url;
        initView(view);
        initData();
        setData(path);
       recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
           @Override
           public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
               super.onScrollStateChanged(recyclerView, newState);
               //判断 RecyclerView是否停止滑动
               // 判断有没有滑动到最后一个item

               if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                   int column = layoutManager.getColumnCountForAccessibility(null, null);
                   int positions[]=new int[column];
                   layoutManager.findLastVisibleItemPositions(positions);
                   for(int i=0;i<positions.length ; i++){

                       if(positions[i] >= (layoutManager.getItemCount()-column)
                               && layoutManager.findViewByPosition(positions[i]).getBottom() <= recyclerView.getHeight()){

                           page++;
                         //  Log.e("------------>", "onScrollStateChanged: " );

                           String format = String.format(UrlConfigs.LIVE_URL, page, index);
                           setData(format);

                       }

                   }


               }
           }
       });


        return view;
    }



    private void initData() {
        layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new LiveInAdapter(getContext(),data);
        adapter.setClickListener(this);
        recyclerView.setAdapter(adapter);
    }

    private void setData(String url) {


        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }
                    @Override
                    public void onResponse(String response, int id) {

                        Gson gson = new Gson();
                        LiveInBean bean = gson.fromJson(response, LiveInBean.class);
                        List<LiveInBean.MessageBean.AnchorsBean> anchors = bean.getMessage().getAnchors();
                        data.addAll(anchors);



                        adapter.notifyDataSetChanged();


                    }
                });

    }

    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.fragment_live_in_recyclerview);

    }


    @Override
    public void onClick(View v) {
        String format = String.format(UrlConfigs.LIVE_URL, page, index);
        final int tag = (Integer) v.getTag();
        Toast.makeText(getContext(), ""+tag, Toast.LENGTH_SHORT).show();

        if (format != null) {
            OkHttpUtils.get().url(format).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(String response, int id) {
                    Gson gson = new Gson();
                    LiveInBean bean = gson.fromJson(response, LiveInBean.class);
                    List<LiveInBean.MessageBean.AnchorsBean> anchors = bean.getMessage().getAnchors();
                    String roomid = anchors.get(tag).getRoomid();
                    int live = anchors.get(tag).getLive();
                    int push = anchors.get(tag).getPush();

                    Intent intent = new Intent(getContext(), LiveActivity.class);
                    intent.putExtra("tag", tag);
                    intent.putExtra("roomid", roomid);
                   // intent.putExtra("ddd","fd");



                    intent.putExtra("live", live);
                    startActivity(intent);
                }
            });

        }

    }
}
